import QtQuick 2.12
import QtQuick.Controls 2.12
import QtQuick.Layouts 1.2

Page {
    id: mainView
    Keys.enabled: true
    focus: true
    header: ToolBar {
        height: 30
        background: Rectangle {
            implicitWidth: 100
            implicitHeight: 40
            opacity: 1
            color: "#2c489c"
        }
        RowLayout {
            anchors.fill: parent
            Label {
                id: netstate
                text: "网络状态：[-]"
                font.pointSize: 14
                font.weight: Font.DemiBold
                color: '#ffffff'
                elide: Label.ElideRight
                horizontalAlignment: Qt.AlignRight
                verticalAlignment: Qt.AlignVCenter
                Layout.fillWidth: true
            }
        }
    }
    footer: ToolBar {
        height: 70
        background: Rectangle {
            implicitWidth: 100
            implicitHeight: 40
            opacity: 1
            color: "#3f51b5"
        }
        ColumnLayout {
            anchors.fill: parent
            Label {
                text: "欢迎使用芯智答题卡计数系统V2.0"
                font.pointSize: 18
                color: '#ffffff'
                elide: Label.ElideRight
                horizontalAlignment: Qt.AlignHCenter
                verticalAlignment: Qt.AlignBottom
                Layout.fillWidth: true
            }
            Label {
                text: "©Copyright 2022 All Rights Reserved"
                font.pointSize: 14
                color: '#ffffff'
                elide: Label.ElideRight
                horizontalAlignment: Qt.AlignHCenter
                verticalAlignment: Qt.AlignTop
                Layout.fillWidth: true
            }
        }
    }
    Rectangle{
        anchors.fill: parent
        color: '#2c489c'
        GridLayout {
            id: homeMenu
            anchors.fill: parent
            columns: 3
            ColumnLayout {
                Layout.alignment: Qt.AlignHCenter | Qt.AlignVCenter
                Image { source: './imgs/icon-counter.png'; width: 80; height: 80; Layout.alignment: Qt.AlignHCenter}
                Text { 
                    text: '直接计数[1]'
                    color: "white"
                    font.pointSize: 20
                    Layout.alignment: Qt.AlignHCenter
                }
                // MouseArea {
                //     anchors.fill: parent
                //     onClicked: homeMenu.currentIndex = index  //实现item切换
                // }
            }
            ColumnLayout {
                Layout.alignment: Qt.AlignHCenter | Qt.AlignVCenter
                Image { source: './imgs/icon-project.png'; width: 80; height: 80; Layout.alignment: Qt.AlignHCenter}
                Text { 
                    text: '项目计数[2]'
                    color: "white"
                    font.pointSize: 20
                    Layout.alignment: Qt.AlignHCenter
                }
                // MouseArea {
                //     anchors.fill: parent
                //     onClicked: homeMenu.currentIndex = index  //实现item切换
                // }
            }
            ColumnLayout {
                Layout.alignment: Qt.AlignHCenter | Qt.AlignVCenter
                Image { source: './imgs/icon-setting.png'; width: 80; height: 80; Layout.alignment: Qt.AlignHCenter}
                Text { 
                    text: "设置[3]"
                    color: "white"
                    font.pointSize: 20
                    Layout.alignment: Qt.AlignHCenter
                }
                // MouseArea {
                //     anchors.fill: parent
                //     onClicked: homeMenu.currentIndex = index  //实现item切换
                // }
            }
        }
        
        Keys.enabled: true
        focus: true
        Keys.onPressed: {

            switch (event.key) {
                case Qt.Key_1:
                    // pageCounter.visible = true;
                    // pageCounter.stack = stack;//设置页面栈
                    // stack.push(pageCounter);//入栈
                    stack.custPush('PageCounter.qml')
                    break;
                case Qt.Key_2:
                    // pageTask1.visible = true;
                    // pageTask1.stack = stack;//设置页面栈
                    // stack.push(pageTask1);//入栈
                    stack.custPush('PageTaskA.qml')

                    break;
                case Qt.Key_3:
                    // pageSetting.visible = true;
                    // pageSetting.stack = stack;//设置页面栈
                    // stack.push(pageSetting);//入栈
                    stack.custPush('PageSetting.qml')
                    break;
                case Qt.Key_7:
                    Qt.quit()
                    break;
                case Qt.Key_4:
                    rootWindow.popupTips("提示自动关闭")
                    break;
                default:
                    return
            }
            event.accepted = true
        }
        Keys.onReleased:{
            
        }
    }
    
    // 作者：五月天山雪Ls https://www.bilibili.com/read/cv18833158 出处：bilibili
    // Connections {
    //     target: person2 // 绑定python的类
    //     function onNetstateSig(name, value){ // 使用 on + 信号名(首字母大写) 作为信号的槽
    //         console.log('收到信号, name:', name, ', value:', value)
    //     }
    // } 
    Component.onCompleted:{
        // gNetwork.netstateSig.connect(mainView.afterNetchange)
        // mainView.afterNetchange.connect(gNetwork.netstateSig)
    }
    Connections{
        target: gNetwork
        function onNetstateSig(connected){
            // console.log(connected)
            if(connected){
                netstate.text = '网络状态：[已连接]';
                netstate.color = '#12fd5a';
            }else{
                netstate.text = '网络状态：[未连接]';
                netstate.color = '#ff4246';
            }
        }
    }
}